package com.money.demo.runner;

import com.money.dao.MoneyDetailMapper;
import com.money.dao.MoneyMapper;
import com.money.dao.PersonMapper;
import com.money.demo.thread.Consumer;
import com.money.demo.thread.Producer;
import com.money.domain.Money;
import com.money.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class StartupRunner implements ApplicationRunner {


    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private MoneyMapper moneyMapper;
    @Autowired
    private MoneyDetailMapper moneyDetailMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(111);

        int index1=10;
        int index2=20;
        Random random=new Random(System.currentTimeMillis());


//        List<Person> persons=new ArrayList<>();
//        for(int i=1; i<=index2; i++){
//            Person person=new Person();
//            person.setId(333+i);
//            person.setName("用户"+(333+i));
//            person.setGroupId(i%2+1);
//            person.setGroupName("红包群"+(i%2+1));
//            persons.add(person);
//            personMapper.insert(person);
//
//        }
//
//        List<Money> monies=new ArrayList<>();
//        for(int i=1; i<=index1; i++){
//            Money money=new Money();
//            money.setId(System.currentTimeMillis());
//            money.setSender(333+i);
//            money.setSenderName("用户"+(333+i));
//            money.setGroupId(i%2+1);
//            money.setGroupName("红包群"+(i%2+1));
//            money.setTotal(random.nextInt(10000)/100.0);
//            money.setCount(random.nextInt(20));
//            money.setCost(0.0);
//            money.setTime(new Date());
//            while (money.getTotal()==0 || money.getCount()==0){
//                money.setTotal(random.nextInt(10000)/100.0);
//                money.setCount(random.nextInt(20));
//            }
//            monies.add(money);
//            moneyMapper.insert(money);
//        }



        ExecutorService executor= Executors.newFixedThreadPool(100);

        List<Money> monies=moneyMapper.selectAll();

        for (Money money : monies) {
            executor.submit(new Producer(money));
        }


        List<Person> persons=personMapper.selectAll();
        for (Person person : persons) {
            executor.submit(new Consumer(person, moneyDetailMapper, personMapper));
        }



    }
}
